'use strict';

const n = 20;

let partis, lastt = 0;

function lop(ts) {
    if (ts - lastt > 60) {
        lastt = ts;
        let i = partis.length, r, tmp;
        while (0 != i) {
            r = Math.floor(Math.random()*i);
            i--;
            tmp = partis[i];
            partis[i] = partis[r];
            partis[r] = tmp;
        }
        rolling.innerHTML = partis.slice(0,20).join(' ');
    }
    iid = requestAnimationFrame(lop);
};

let swch = true, iid, t = 0;

chou.onclick = ()=>{
    if (swch) {
        if (t>=3) {
            chou.innerHTML = '抽奖已结束';
            chou.style.color = 'darkred';
            parti.readOnly = false;
            return;
        }
        iid = requestAnimationFrame(lop);
        chou.innerHTML = '停！';
        parti.readOnly = true;
        t++;
    } else {
        cancelAnimationFrame(iid);
        chou.innerHTML = '抽奖';
        if (t==1) {
            out1.innerHTML = rolling.innerHTML;
        } else if (t==2) {
            out2.innerHTML = rolling.innerHTML;
        } else
            out3.innerHTML = rolling.innerHTML;
        partis = partis.slice(n);
    }
    swch = !swch;
};

parti.onchange = ()=>{
    partis = parti.value.split(/\s+/g);
    pts.innerHTML = '参与人员（共' + partis.length + '人）';
    cancelAnimationFrame(iid);
    rolling.innerHTML =
    out1.innerHTML = 
    out2.innerHTML =
    out3.innerHTML = '……';
    chou.innerHTML = '抽奖';
    chou.style.color = '';
    t = lastt = 0;
};

parti.onchange();

function reset() {
    if (confirm('确认重置中奖结果？')) {
        parti.onchange();
        parti.readOnly = false;
    }
}